package com.xjtu.platform.admin.impl;

import com.github.pagehelper.PageInfo;
import com.xjtu.platform.admin.ReaderService;
import com.xjtu.platform.admin.dao.AddReaderDao;
import com.xjtu.platform.admin.dto.*;
import com.xjtu.platform.admin.entity.ReaderBaseInfo;
import com.xjtu.platform.admin.entity.ReaderListEntity;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.common.entity.FileEntity;
import com.xjtu.platform.common.entity.PrimaryKey;
import com.xjtu.platform.common.page.QueryAction2;
import com.xjtu.platform.reader.entity.Author;
import com.xjtu.platform.reader.entity.BookBaseInfo;
import com.xjtu.platform.reader.entity.BookLabel;
import com.xjtu.platform.reader.entity.BookListEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: platform
 * @description: 新增读者
 * @author: Wushanxu
 * @create: 2023-11-18 23:29
 **/
@Service
@Transactional
public class AdminReaderServiceImpl implements ReaderService {

    @Resource
    private AddReaderDao addReaderDao;

    @Override
    public boolean addReader(AddReaderDto addReaderDto) {
        try {
            PrimaryKey primaryKey = new PrimaryKey();
            addReaderDao.addNewReader(addReaderDto, primaryKey);
            String pkId = primaryKey.getId();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteReader(DeleteReaderDto deleteReaderDto) {
        try {
            addReaderDao.deleteReader(deleteReaderDto);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map<String, Object> getReaderList(GetReaderListParamDto dto) {
        return Utils.getPage2(dto, () -> {
            try {
                System.out.println(dto.toString());
                List<ReaderBaseInfo> baseList = addReaderDao.getReaderBaseInfoByParam(dto);
                PageInfo<ReaderBaseInfo> page = new PageInfo<>(baseList);
                List<ReaderListEntity> list = new ArrayList<>();
                for (ReaderBaseInfo item : baseList) {
                    ReaderListEntity readerListEntity = new ReaderListEntity();
                    readerListEntity.setReaderBaseInfo(item);
                    list.add(readerListEntity);
                }
                PageInfo<ReaderListEntity> page2 = new PageInfo<>();
                BeanUtils.copyProperties(page, page2);
                page2.setList(list);
                return page2;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        });
    }

    @Override
    public boolean editReader(EditReaderDto editReaderDto) {
        try {
            PrimaryKey primaryKey = new PrimaryKey();
            addReaderDao.modifyBook(editReaderDto, primaryKey);
            String pkId = primaryKey.getId();
            System.out.println("pk:" + primaryKey.getId());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map<String, Object> getBookList(GetBookListDto dto) {
        return Utils.getPage2(dto, () -> {
            try {
                System.out.println(dto.toString());
                List<BookBaseInfo> baseList;
                if (dto.getIsEmpty() == 0) {
                    baseList = addReaderDao.getBookBaseInfoByParam(dto);
                } else {
                    baseList = addReaderDao.getEmptyBookBaseInfoByParam(dto);
                }
                PageInfo<BookBaseInfo> page = new PageInfo<>(baseList);
                List<BookListEntity> list = new ArrayList<>();
                for (BookBaseInfo item : baseList) {
                    BookListEntity bookListEntity = new BookListEntity();
                    bookListEntity.setBookBaseInfo(item);

                    List<FileEntity> fileEntity = addReaderDao.getBookPic(item.getBook_id());
                    for (FileEntity s : fileEntity) {
                        s.setFile_url(Utils.transAddToUrl(s.getFile_url()));
                    }
                    bookListEntity.setFileList(fileEntity);

                    List<Author> authorList = addReaderDao.getBookAuthors(item.getBook_id());
                    bookListEntity.setAuthorList(authorList);

                    List<BookLabel> bookLabelList = addReaderDao.getBookLabelList(item.getBook_id());
                    bookListEntity.setLabelList(bookLabelList);

                    bookListEntity.setBook_class(addReaderDao.getBookClass(item.getBook_id()));
                    list.add(bookListEntity);
                }
                PageInfo<BookListEntity> page2 = new PageInfo<>();
                BeanUtils.copyProperties(page, page2);
                page2.setList(list);
                return page2;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        });
    }
}
